<template>
  <div class="mod-seckill-shopAuditing">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row margin-bottom-none">
          <el-form-item
            prop="shopName"
            :label="$t('shop.storeName') + ':'"
          >
            <el-input
              v-model="searchForm.shopName"
              clearable
              :placeholder="$t('shop.storeName')"
            />
          </el-form-item>
          <el-form-item
            :label="$t('shop.applicationDate') + ':'"
          >
            <el-date-picker
              v-model="dateRange"
              type="datetimerange"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                              new Date(2000, 2, 1, 23, 59, 59)]"
              :range-separator="$t('date.tip')"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('product.search') }}
            </div>
            <div
              class="default-btn"
              @click="resetList"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con audit-shop-table">
        <el-table
          :key="tabValue"
          ref="auditShopRef"
          v-loading="dataListLoading"
          :data="dataList"
          header-cell-class-name="table-header"
          style="width: 100%"
        >
          <el-table-column
            prop="shopName"
            :label="$t('shop.storeName')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.shopName || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="intro"
            :label="$t('shop.shopProfile')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.shopIntro || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            :label="$t('shop.applicationDate')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.createTime || '-' }}</span>
            </template>
          </el-table-column>

          <el-table-column
            :label="$t('publics.operating')"
            width="150px"
          >
            <template #default="scope">
              <div
                v-if="isAuth('shop:companyAuditing:audit')"
                class="default-btn text-btn"
                @click="auditHandle(scope.row.shopId)"
              >
                {{ $t('shopProcess.audit') }}
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        :current-page="page.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>

    <!-- 审核操作 -->
    <auditBusinessInfor
      v-if="addOrUpdateVisible"
      ref="auditBusinessRef"
      @close-pops="closeAuditBusiness"
    />
  </div>
</template>

<script setup>
import auditBusinessInfor from '../audit-business-infor/index.vue'
import { isAuth } from '@/utils'

defineProps({
  tabValue: {
    type: String,
    default: ''
  }
})

const page = reactive({
  total: 0, // 总页数
  current: 1, // 当前页数
  size: 10 // 每页显示多少条
})

// 搜索
const searchForm = reactive({
  shopName: '', // 商城名
  beginTime: '', // 开始时间
  endTime: '' // 结束时间
})

onMounted(() => {
  getDataList()
})

const dataListLoading = ref(false) // 加载框
const dataList = ref([]) // 申请数据
const getDataList = () => {
  const param = {}
  Object.assign(param, searchForm)
  param.size = page.size
  param.current = page.current
  dataListLoading.value = true
  http({
    url: http.adornUrl('/platform/companyAuditing/page'),
    method: 'get',
    params: http.adornParams(param)
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
  }).finally(() => {
    dataListLoading.value = false
  })
}

const dateRange = ref([]) // 申请时间
// 重置
const resetList = () => {
  for (const i in searchForm) {
    searchForm[i] = ''
  }
  dateRange.value = ''
  page.current = 1
}

const addOrUpdateVisible = ref(false)
const auditBusinessRef = ref(null)
// 查看审核
const auditHandle = (id) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    auditBusinessRef.value?.init(id)
  })
}
// 关闭审核弹窗
const closeAuditBusiness = () => {
  addOrUpdateVisible.value = false
  getDataList()
}

// 检索
const onSearch = () => {
  searchForm.beginTime = ''
  searchForm.endTime = ''
  if (dateRange.value && dateRange.value.length !== 0) {
    searchForm.beginTime = dateRange.value[0]
    searchForm.endTime = dateRange.value[1]
  }
  page.current = 1
  getDataList()
}

// 修改分页
const onPageSizeChange = (val) => {
  page.size = val
  getDataList()
}

const onPageChange = (val) => {
  page.current = val
  getDataList()
}

</script>
